package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.PaginationVO;
import com.itheima.dto.*;
import com.itheima.entity.*;
import com.itheima.mapper.ConfigMapper;
import com.itheima.mapper.HomeMapper;
import com.itheima.mapper.TagMapper;
import com.itheima.service.HomeService;
import com.itheima.vo.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
@Slf4j
@RequiredArgsConstructor
public class HomeServiceImpl implements HomeService {
    private final HomeMapper homeMapper;
    private final ConfigMapper configMapper;
    private final TagMapper tagMapper;

    @Override
    public FrontHomeVO getAllList() {
        List<YddCate> cateList = homeMapper.getAllCate();
        List<contentVO> contentList = new ArrayList<>();
        cateList.forEach(cate -> {
            contentVO content = cate.getCateVO();
            contentList.add(content);
        });
        FrontHomeVO frontHomeVO = new FrontHomeVO();
        frontHomeVO.setAllCate(contentList);

        Set<String> set = configMapper.getTob();
        FrontConfigVO frontConfigVO = new FrontConfigVO();
        List<ConfigurationDTO_Data> list = new ArrayList<>();
        for (String s : set) {
            list.add(getList(s));
            frontConfigVO.setList(list);
        }

        List<AllData> allData = configMapper.getAttr();
        List<AllDataDTO> allDataDTOList = new ArrayList<>();
        allData.forEach(s -> allDataDTOList.add(s.getAllDataDTO()));
        frontConfigVO.setAllData(allDataDTOList);
        frontHomeVO.setConfig(frontConfigVO);

        List<YddTag> tagList = tagMapper.getAll();
        frontHomeVO.setAllTag(tagList);

        List<LinkVO> linkList = homeMapper.getAllLink();
        linkList.forEach(s->{
            s.setDefault_data("1");
        });
        frontHomeVO.setAllLink(linkList);

        List<BannerVO> bannerList = homeMapper.getAllBannerList();
        frontHomeVO.setBanner(bannerList);
        frontHomeVO.setSideBanner(bannerList);
        frontHomeVO.setShowBanner(bannerList);
        return frontHomeVO;
    }

    @Override
    public EssayDataVO getAllEssayList(Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<YddEssay> essays = homeMapper.list();
        System.out.println(essays);
        PageInfo<YddEssay> pageInfo = new PageInfo<>(essays);
        long count = pageInfo.getTotal();
        int totalPages = pageInfo.getPages();
        int pageSize = pageInfo.getPageSize();
        int currentPage = pageInfo.getNextPage();
        List<YddEssay> essayList = pageInfo.getList();
        List<EssayVO> essayVOList = new ArrayList<>();
        essayList.forEach(essay -> essayVOList.add(essay.getEssayVO()));
        PageVo<EssayVO> paginationVO = new PageVo<>();
        paginationVO.setCount(count);
        paginationVO.setTotalPages(totalPages);
        paginationVO.setPageSize(pageSize);
        paginationVO.setCurrentPage(currentPage);
        paginationVO.setData(essayVOList);
        EssayDataVO essayDataVO = new EssayDataVO();
        essayDataVO.setDesc("首页大列表");
        essayDataVO.setNum_day(0);
        essayDataVO.setNum_week(0);
        essayDataVO.setList(paginationVO);
        return essayDataVO;
    }

    @Override
    public HotEssayAllVO getAllHotEssayList() {
        HotEssayAllVO hotEssayAllVO = new HotEssayAllVO();
        List<HotEssayVO> hotEssayVOS = homeMapper.getAllHotEssayList();
        hotEssayAllVO.setDesc("侧边最新文章");
        hotEssayAllVO.setList(hotEssayVOS);
        return hotEssayAllVO;
    }

    @Override
    public HotEssayAllVO getAllTopEssayList() {
        HotEssayAllVO topEssayAllVO = new HotEssayAllVO();
        List<HotEssayVO> hotEssayVOS = homeMapper.getAllTopEssayList();
        topEssayAllVO.setDesc("侧边推荐文章");
        topEssayAllVO.setList(hotEssayVOS);
        return topEssayAllVO;
    }

    @Override
    public AllListVO getList(Integer page, Integer limit, Integer cateid) {
        PageHelper.startPage(page, limit);
        List<YddEssay> essays = homeMapper.getList(cateid);
        System.out.println(essays);
        PageInfo<YddEssay> pageInfo = new PageInfo<>(essays);
        long count = pageInfo.getTotal();
        int totalPages = pageInfo.getPages();
        int pageSize = pageInfo.getPageSize();
        int currentPage = pageInfo.getNextPage();
        List<YddEssay> essayList = pageInfo.getList();
        List<EssayVO> essayVOList = new ArrayList<>();
        essayList.forEach(essay -> essayVOList.add(essay.getEssayVO()));
        PageVo<EssayVO> paginationVO = new PageVo<>();
        paginationVO.setCount(count);
        paginationVO.setTotalPages(totalPages);
        paginationVO.setPageSize(pageSize);
        paginationVO.setCurrentPage(currentPage);
        paginationVO.setData(essayVOList);
        AllListVO allListVO = new AllListVO();
        allListVO.setList(paginationVO);
        return allListVO;
    }

    @Override
    public ShowEssayVO getShowList(Integer id) {
        ShowEssayVO showEssayVO = new ShowEssayVO();
        ShowVO showVO1 = homeMapper.getShowVO(id + 1);
        ShowVO showVO2 = homeMapper.getShowVO(id - 1);
        ShowInfo showInfo = homeMapper.getShowInfoVO(id);
        ShowInfoVO showInfoVO = new ShowInfoVO();
        BeanUtils.copyProperties(showInfo, showInfoVO);
        showInfoVO.setCreate_date(showInfo.getCreateDate());
        showInfoVO.setUpdate_date(showInfo.getUpdateDate());
        showInfoVO.setUser_id(showInfo.getUserId());
        showInfoVO.setIsLink(false);
        showInfoVO.setIsCollect(false);
        List<recommendVO> recommendVO = homeMapper.getRecommendVO(showInfoVO.getCateid());
        showEssayVO.setPrev(showVO2);
        showEssayVO.setNext(showVO1);
        showEssayVO.setInfo(showInfoVO);
        showEssayVO.setRecommend(recommendVO);
        return showEssayVO;
    }

    private ConfigurationDTO_Data getList(String s) {
        ConfigurationDTO_Data cd = new ConfigurationDTO_Data();
        Set<String> setTag = configMapper.getGroupValue(s);
        switch (s) {
            case "base":
                cd.setName("基础配置");
                break;
            case "custom":
                cd.setName("其他配置");
                break;
            case "system":
                cd.setName("系统配置");
                break;
        }
        cd.setKey(s);
        List<SubseDTO> list1 = new ArrayList<>();
        for (String str : setTag) {
            SubseDTO subseDTO = new SubseDTO();
            subseDTO.setName(str);
            List<Group> strGroup = configMapper.getAllGroup(str, s);
            List<GroupDTO> groupDTOS = new ArrayList<>();
            strGroup.forEach(e -> {
                groupDTOS.add(e.getGroupDTO());
            });
            subseDTO.setData(groupDTOS);
            if (!subseDTO.getData().isEmpty()) {
                list1.add(subseDTO);
            }
        }
        cd.setChildren(list1);
        return cd;
    }
}
